<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    
    
    <title>php获取客户端IP | 追光者的部落 | 种一棵树最好的时间是十年之前,其次是现在</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="keywords" content="PHP">
    <link rel="shortcut icon" href="/img/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.0.0">
    
    <script type="text/javascript">
        // Data Center
        var DC = {
            reward:	true,
            lv: JSON.parse('{"enable":false,"app_id":null,"app_key":null,"icon":true}'),
            v: JSON.parse('{"enable":true,"appid":"OCT05YTTqCrexJcyc5Ay0XXD-gzGzoHsz","appkey":"L6b0lDuLhBWOEUhEYmfA7g7C","notify":false,"verify":false,"placeholder":"suggest","avatar":"wavatar"}'),
            g: JSON.parse('{"enable":false,"lazy":true,"owner":"longmartin","repo":null,"oauth":{"client_id":null,"client_secret":null},"perPage":10}'),
            d: JSON.parse('{"app_id":null}')
        };
    </script>
    <script type="text/javascript">
        window.lazyScripts=[];
    </script>
    
</head>


<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      
      <img src="/img/brand.jpg" class="brand-bg">
      
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Martin Long</h5>
          <a href="mailto:1156341485@qq.com" title="1156341485@qq.com" class="mail">
            
              <span>1</span>
            
              <span>1</span>
            
              <span>5</span>
            
              <span>6</span>
            
              <span>3</span>
            
              <span>4</span>
            
              <span>1</span>
            
              <span>4</span>
            
              <span>8</span>
            
              <span>5</span>
            
              <span>@</span>
            
              <span>q</span>
            
              <span>q</span>
            
              <span>.</span>
            
              <span>c</span>
            
              <span>o</span>
            
              <span>m</span>
            
          </a>
        </hgroup>
        
        <ul class="menu-link">
          
              <li>
                <a href="https://github.com/longmartin" target="_blank">
                  <i class="icon icon-lg icon-github"></i>
                </a>
              </li>
            
        </ul>
        
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                HOME
              </a>
            </li>
        
            <li class="">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                文档
              </a>
            </li>
        
            <li class="">
              <a href="https://github.com/longmartin" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="">
              <a href="https://www.jianshu.com/u/696184ca9e21" target="_blank" >
                <i class="icon icon-lg icon-link"></i>
                简书
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row clearfix">
        <a href="javascript:;" class="header-icon pull-left waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">
            <span>php获取客户端IP</span>
            
        </div>
        
        <a href="javascript:;" id="site_search_btn" class="header-icon pull-right waves-effect waves-circle waves-light">
            <i class="icon icon-lg icon-search"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">
    <div class="container fade-scale">
        <h1 class="title">php获取客户端IP</h1>
        <h5 class="subtitle">
            
                <time datetime="2018-06-17T11:02:52.000Z" itemprop="datePublished" class="page-time">
  2018-06-17
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/server/">后台</a></li></ul>

            
        </h5>
        
    </div>
    

</header>

<div id="site_search">
    <div class="search-title clearfix">
        <span class="pull-left">
          <i class="icon icon-lg icon-search"></i>
        </span>
        <input type="text" id="local-search-input" name="q" results="0" placeholder="search my blog..." class="form-control pull-left"/>
        <a href="javascript:;" class="close pull-right waves-effect waves-circle waves-light">
          <i class="icon icon-lg icon-close"></i>
        </a>
    </div>
    <div id="local-search-result"></div>
</div>


<div class="container body-wrap">
    <article id="post-20180617-16"
  class="post-article article-type-post" itemprop="blogPost">
    <div class="post-card">
        <h1 class="post-card-title">php获取客户端IP</h1>
        <div class="post-meta">
            <time class="post-time" title="2018-06-17 11:02:52" datetime="2018-06-17T11:02:52.000Z"  itemprop="datePublished">2018-06-17</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/server/">后台</a></li></ul>



            

            
    <span class="leancloud-comment">
        <i class="icon icon-comment-o"></i>
        <a href="/server/20180617-16.html#comment">
            <span class="valine-comment-count" data-xid="/server/20180617-16.html"></span>
        </a>
    </span>



            
        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            
            <h3 id="比较-HTTP-CLIENT-IP-HTTP-X-FORWARDED-FOR-REMOTE-ADDR"><a href="#比较-HTTP-CLIENT-IP-HTTP-X-FORWARDED-FOR-REMOTE-ADDR" class="headerlink" title="比较  HTTP_CLIENT_IP HTTP_X_FORWARDED_FOR REMOTE_ADDR"></a>比较  <code>HTTP_CLIENT_IP</code> <code>HTTP_X_FORWARDED_FOR</code> <code>REMOTE_ADDR</code></h3><p>最近做项目的时候，遇到一个需要记录客户端ip的，后台语言是php,我想了一下，用<code>REMOTE_ADDR</code>吧。可是呢，后来发现其实这个还有很多其他的获取方式，<code>HTTP_CLIENT_IP</code>, <code>HTTP_X_FORWARDED_FOR</code>都可以获取，我仔细查了一下资料，总结一下这三者的区别，另附完整解决方法：</p>
<ol>
<li><code>HTTP_CLIENT_IP</code>头是有的，只是未成标准，不一定服务器都实现了。</li>
<li><code>HTTP_X_FORWARDED_FOR</code> 是有标准定义，用来识别经过<code>HTTP代理</code>后的客户端IP地址，格式：<code>clientip,proxy1,proxy2</code>。详细解释见 <a href="http://zh.wikipedia.org/wiki/X-Forwarded-For" target="_blank" rel="noopener">http://zh.wikipedia.org/wiki/X-Forwarded-For</a></li>
<li><code>REMOTE_ADDR</code> 是可靠的， 它是最后一个跟你的服务器握手的<code>IP</code>，可能是用户的代理服务器，也可能是自己的反向代理。</li>
</ol>
<p>顺便说下$_SERVER和getenv的区别，getenv不支持IIS的isapi方式运行的php<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">function getIp() &#123;</span><br><span class="line">    if (getenv ( &quot;HTTP_CLIENT_IP&quot; ) &amp;&amp; strcasecmp ( getenv ( &quot;HTTP_CLIENT_IP&quot; ), &quot;unknown&quot; )) &#123;</span><br><span class="line">        $ip = getenv ( &quot;HTTP_CLIENT_IP&quot; );</span><br><span class="line">    &#125; elseif (getenv ( &quot;HTTP_X_FORWARDED_FOR&quot; ) &amp;&amp; strcasecmp ( getenv ( &quot;HTTP_X_FORWARDED_FOR&quot; ), &quot;unknown&quot; )) &#123;</span><br><span class="line">        $ip = getenv ( &quot;HTTP_X_FORWARDED_FOR&quot; );</span><br><span class="line">    &#125; elseif (getenv ( &quot;REMOTE_ADDR&quot; ) &amp;&amp; strcasecmp ( getenv ( &quot;REMOTE_ADDR&quot; ), &quot;unknown&quot; )) &#123;</span><br><span class="line">        $ip = getenv ( &quot;REMOTE_ADDR&quot; );</span><br><span class="line">    &#125; elseif (isset ( $_SERVER [&apos;REMOTE_ADDR&apos;] ) &amp;&amp; $_SERVER [&apos;REMOTE_ADDR&apos;] &amp;&amp; strcasecmp ( $_SERVER [&apos;REMOTE_ADDR&apos;], &quot;unknown&quot; )) &#123;</span><br><span class="line">        $ip = $_SERVER [&apos;REMOTE_ADDR&apos;];</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        $ip = &quot;unknown&quot;;</span><br><span class="line">    &#125;</span><br><span class="line">    return ($ip);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><code>REMOTE_ADDR</code>不可以显式的伪造，虽然可以通过代理将ip地址隐藏，但是这个地址仍然具有参考价值，因为它就是与你的服务器实际连接的ip地址。</p>
<p>相比之下，前两种ip地址都可以通过http header来伪造，但并不意味着它们一无是处。生产环境中很多服务器隐藏在负载均衡节点后面，你通过<code>REMOTE_ADDR</code>只能获取到负载均衡节点的ip地址，一般的负载均衡节点会把前端实际的ip地址通过<code>HTTP_CLIENT_IP</code>或者<code>HTTP_X_FORWARDED_FOR</code>这两种http头传递过来</p>

        </div>
        
<blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    Last updated: <time datetime="2018-06-17T03:08:18.422Z" itemprop="dateUpdated">2018-06-17 03:08:18</time>
</span><br>


        
        转载注明出处，原文地址：<a href="/server/20180617-16.html" target="_blank" rel="external">http://blog.inbelieve.top/server/20180617-16.html</a>
        
    </div>
    <footer>
        <a href="http://blog.inbelieve.top">
            <img src="/img/avatar.jpg" alt="Martin Long">
            Martin Long
        </a>
    </footer>
</blockquote>

        
            <div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>

            
        
        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/php/">PHP</a></li></ul>

            <div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://blog.inbelieve.top/server/20180617-16.html&title=《php获取客户端IP》 — 追光者的部落&source=有些鸟儿天生是没办法被关在笼子里的,因为他的每一片羽毛都闪耀着自由的光辉" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>

        </div>
        
            
    <div id="comment"></div>



        
    </div>
    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="prev">
      <a href="/server/20180617-17.html" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Composer设置忽略版本匹配</h4>
      </a>
    </div>
  

  
    <div class="next">
      <a href="/frontend/20180617-15.html" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">js数组之forEach,every,some,filter,map的区别</h4>
      </a>
    </div>
  
</nav>


    
    
        <aside class="post-widget">
            <nav class="post-toc-wrap" id="post-toc">
                <strong>目录</strong>
                <ol class="post-toc"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#比较-HTTP-CLIENT-IP-HTTP-X-FORWARDED-FOR-REMOTE-ADDR"><span class="post-toc-number">1.</span> <span class="post-toc-text">比较  HTTP_CLIENT_IP HTTP_X_FORWARDED_FOR REMOTE_ADDR</span></a></li></ol>
            </nav>
            <div class="toc-bar"><div>
        </aside>
    
</article>

    <div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
            请作者喝杯咖啡
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>

    
</div>

        <footer class="footer">
    
    <div class="bottom">
        <p>
            <span>
                Martin Long &copy; 2017 - 2018
            </span>
        		
           	
            
            
            <span>
	            Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/codefine/hexo-theme-mellow" target="_blank">book</a>
            </span>
            
            
            
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://blog.inbelieve.top/server/20180617-16.html&title=《php获取客户端IP》 — 追光者的部落&source=有些鸟儿天生是没办法被关在笼子里的,因为他的每一片羽毛都闪耀着自由的光辉" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>


    
    <!-- main-js -->
<script type="text/javascript" src="//cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript" src="/js/plugins/fastclick.js?v=1.0.0"></script>

<script type="text/javascript" src="https://cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>

<script type="text/javascript" src="/js/method.js?v=1.0.0"></script>
<script type="text/javascript" src="/js/blog.js?v=1.0.0"></script>

<!-- third-party -->





<script type="text/javascript" src="/js/plugins/local_search.js?v=1.0.0"></script>
<script type="text/javascript">
	var search_path = "search.xml";
	if (search_path.length === 0) {
		search_path = "search.xml";
	}
	var path = "/" + search_path;
	searchFunc(path, "local-search-input", "local-search-result");
</script>



    
        <script type="text/javascript" src="https://cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script type="text/javascript" src="//unpkg.com/valine/dist/Valine.min.js"></script>
<script type="text/javascript" src="/js/plugins/valine.js?v=1.0.0"></script>
    
    







    <script>
    (function() {
        var OriginTitile = document.title, titleTime;
        document.addEventListener('visibilitychange', function() {
            if (document.hidden) {
                document.title = 'leaving！';
                clearTimeout(titleTime);
            } else {
                document.title = 'welcome!';
                titleTime = setTimeout(function() {
                    document.title = OriginTitile;
                },2000);
            }
        });
    })();
</script>




    
</body>
</html>
